package com.sgb.member.util;

import static com.amap.api.maps.model.BitmapDescriptorFactory.getContext;

import android.content.Context;
import android.graphics.Color;
import android.text.TextUtils;
import android.util.SparseArray;
import android.util.SparseIntArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.core.content.ContextCompat;

import com.google.android.material.tabs.TabLayout;
import com.sgb.member.PickCateType;
import com.sgb.member.R;
import com.sgb.member.weight.SimpleResult;
import com.swgk.core.util.ResolutionUtil;
import com.swgk.core.view.selection.CommonPopEntity;
import com.swgk.core.view.selection.XChooseDialog;
import com.swgk.core.view.selection.XChooseEntity;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

public class TenderUtil {

    public static void setImageResources(int i, ImageView imageView, boolean isChoose, Context context) {
        int drawableId = R.drawable.icon_qb;
        switch (i) {
            case 0:
                if (isChoose) {
                    drawableId = R.drawable.icon_qb_xz;
                } else {
                    drawableId = R.drawable.icon_qb;
                }
                break;
            case 1:
                if (isChoose) {
                    drawableId = R.drawable.icon_spcl_xz;
                } else {
                    drawableId = R.drawable.icon_spcl;
                }
                break;
            case 2:
                if (isChoose) {
                    drawableId = R.drawable.icon_zygc_xz;
                } else {
                    drawableId = R.drawable.icon_zygc;
                }
                break;
            case 3:
                if (isChoose) {
                    drawableId = R.drawable.icon_lwfb_xz;
                } else {
                    drawableId = R.drawable.icon_lwfb;
                }
                break;
            case 4:
                if (isChoose) {
                    drawableId = R.drawable.icon_gczb_xz;
                } else {
                    drawableId = R.drawable.icon_gczb;
                }
                break;
            case 5:
                if (isChoose) {
                    drawableId = R.drawable.icon_ppp_xz;
                } else {
                    drawableId = R.drawable.icon_ppp;
                }
                break;
            case 6:
                if (isChoose) {
                    drawableId = R.drawable.icon_jxzl_xz;
                } else {
                    drawableId = R.drawable.icon_jxzl;
                }
                break;
            case 7:
                if (isChoose) {
                    drawableId = R.drawable.icon_sbzl_xz;
                } else {
                    drawableId = R.drawable.icon_sbzl;
                }
                break;
            case 8:
                if (isChoose) {
                    drawableId = R.drawable.icon_tdcqzr_xz;
                } else {
                    drawableId = R.drawable.icon_tdcqzr;
                }
                break;
            case 9:
                if (isChoose) {
                    drawableId = R.drawable.icon_fczr_xz;
                } else {
                    drawableId = R.drawable.icon_fczr;
                }
                break;
            case 10:
                if (isChoose) {
                    drawableId = R.drawable.icon_sjgh_xz;
                } else {
                    drawableId = R.drawable.icon_sjgh;
                }
                break;
            case 11:
                if (isChoose) {
                    drawableId = R.drawable.icon_zyjl_xz;
                } else {
                    drawableId = R.drawable.icon_zyjl;
                }
                break;
            case 12:
                if (isChoose) {
                    drawableId = R.drawable.icon_dzkc_xz;
                } else {
                    drawableId = R.drawable.icon_dzkc;
                }
                break;
            case 13:
                if (isChoose) {
                    drawableId = R.drawable.icon_zjzx_xz;
                } else {
                    drawableId = R.drawable.icon_zjzx;
                }
                break;
            case 14:
                if (isChoose) {
                    drawableId = R.drawable.icon_fczl_xz;
                } else {
                    drawableId = R.drawable.icon_fczl;
                }
                break;
            case 15:
                if (isChoose) {
                    drawableId = R.drawable.icon_clzl_xz;
                } else {
                    drawableId = R.drawable.icon_clzl;
                }
                break;
            case 16:
                if (isChoose) {
                    drawableId = R.drawable.icon_jxzr_xz;
                } else {
                    drawableId = R.drawable.icon_jxzr;
                }
                break;
            case 17:
                if (isChoose) {
                    drawableId = R.drawable.icon_sbzr_xz;
                } else {
                    drawableId = R.drawable.icon_sbzr;
                }
                break;
            case 18:
                if (isChoose) {
                    drawableId = R.drawable.icon_clzr_xz;
                } else {
                    drawableId = R.drawable.icon_clzr;
                }
                break;
            case 19:
                if (isChoose) {
                    drawableId = R.drawable.icon_wlhy_xz;
                } else {
                    drawableId = R.drawable.icon_wlhy;
                }
                break;
            case 20:
                if (isChoose) {
                    drawableId = R.drawable.icon_qt_xz;
                } else {
                    drawableId = R.drawable.icon_qt;
                }
                break;
            default:
                break;
        }
        imageView.setBackgroundResource(drawableId);
        imageView.setScaleX(0);
        imageView.setScaleY(0);
        float scale;
        if (isChoose) {
            scale = ResolutionUtil.dp2px(context, 45) / (float) ResolutionUtil.dp2px(context, 36);
        } else {
            scale = ResolutionUtil.dp2px(context, 36) / (float) ResolutionUtil.dp2px(context, 36);
        }
        imageView.setScaleX(scale);
        imageView.setScaleY(scale);
    }

    public static void tenderType1(TabLayout tabLayout, List<CommonPopEntity> data, SimpleResult<CommonPopEntity> simpleResult) {
        tabLayout.clearOnTabSelectedListeners();
        tabLayout.removeAllTabs();
        for (int i = 0; i < data.size(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab == null) {
                tab = tabLayout.newTab().setCustomView(R.layout.item_tender_category);
                tabLayout.addTab(tab);
            }
            if (tab.getCustomView() != null) {
                refreshTypeView(tab.getCustomView(), data.get(i), i, i == 0);
            }
        }
        tabLayout.smoothScrollBy(0, 0);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getCustomView() != null) {
                    refreshTypeView(tab.getCustomView(), data.get(tab.getPosition()), tab.getPosition(), true);
                }
                simpleResult.onResult(data.get(tab.getPosition()), tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                if (tab.getCustomView() != null) {
                    refreshTypeView(tab.getCustomView(), data.get(tab.getPosition()), tab.getPosition(), false);
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    private static void refreshTypeView(View view, CommonPopEntity entity, int i, boolean isChoose) {
        TextView textView = view.findViewById(R.id.tv_name);
        ImageView imageView = view.findViewById(R.id.ic_image);
        textView.setText(entity.getTypeName());
        TenderUtil.setImageResources(i, imageView, isChoose, imageView.getContext());
        textView.getPaint().setFakeBoldText(isChoose);
        textView.setTextColor(isChoose ? Color.parseColor("#EF4033") : Color.parseColor("#666666"));
    }

    public static void tenderType2(TabLayout tabLayout, List<CommonPopEntity> data, SimpleResult<CommonPopEntity> simpleResult) {
        tabLayout.clearOnTabSelectedListeners();
        tabLayout.removeAllTabs();
        for (int i = 0; i < data.size(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            if (tab == null) {
                tab = tabLayout.newTab().setCustomView(R.layout.item_tender_category2);
                tabLayout.addTab(tab);
            }
            if (tab.getCustomView() != null) {
                refreshTypeView2(tab.getCustomView(), data.get(i), i, i == 0);
            }
        }
        tabLayout.scrollTo(0, 0);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (tab.getCustomView() != null) {
                    refreshTypeView2(tab.getCustomView(), data.get(tab.getPosition()), tab.getPosition(), true);
                }
                simpleResult.onResult(data.get(tab.getPosition()), tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                if (tab.getCustomView() != null) {
                    refreshTypeView2(tab.getCustomView(), data.get(tab.getPosition()), tab.getPosition(), false);
                }
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

    }

    private static void refreshTypeView2(View view, CommonPopEntity entity, int i, boolean isChoose) {
        TextView textView = view.findViewById(R.id.tv_name);
        if (entity.getTypeName().length() > 4) {
            textView.setText(String.format("%s...", entity.getTypeName().substring(0, 4)));
        } else {
            textView.setText(entity.getTypeName());
        }
        textView.setTextColor(isChoose ? Color.parseColor("#EF4033") : Color.parseColor("#333333"));
        textView.setBackgroundResource(isChoose ? R.drawable.shape_blue_stroke_999_bg : R.drawable.search_bg);
    }
}
